// DlgHomeTab.cpp : implementation file
//

#include "stdafx.h"
#include "PhotoEdit.h"
#include "DlgHomeTab.h"
#include "DlgResize.h"
#include "DlgColor.h"
#include "DlgFilter.h"
#include "afxdialogex.h"

// CDlgHomeTab dialog

IMPLEMENT_DYNAMIC(CDlgHomeTab, CDialogEx)

CDlgHomeTab::CDlgHomeTab(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDlgHomeTab::IDD, pParent)
{
	m_ctrResize.SetDropDownMenu(IDR_MENU_EDIT,0);
	m_ctrImageRotation.SetDropDownMenu(IDR_MENU_EDIT,1);
	m_ctrColorEffect.SetDropDownMenu(IDR_MENU_EDIT,2);
	m_ctrFilterEffect.SetDropDownMenu(IDR_MENU_EDIT,3);
}

CDlgHomeTab::~CDlgHomeTab()
{
}

void CDlgHomeTab::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SPLIT_RESIZE, m_ctrResize);
	DDX_Control(pDX, IDC_SPLIT_ROTATION, m_ctrImageRotation);
	DDX_Control(pDX, IDC_SPLIT_COLOR, m_ctrColorEffect);
	DDX_Control(pDX, IDC_SPLIT_FILTER, m_ctrFilterEffect);
}

void CDlgHomeTab::Resize(int w,int h)
{
	m_pPhoto->Resize(w,h);
	m_pDlgParent->Invalidate();
}
void CDlgHomeTab::RefreshDraw()
{
	m_pDlgParent->Invalidate(FALSE);
}
void CDlgHomeTab::Resize(int l)
{
	int w = m_pPhoto->GetWidth();
	int h = m_pPhoto->GetHeight();
	float scale;
	if(w>h)
	{
		scale = (float)l/w;
	}
	else
	{
		scale = (float)l/h;
	}
	w = (int)(w*scale+0.5);
	h = (int)(h*scale+0.5);
	m_pPhoto->Resize(w,h);
	
}

BEGIN_MESSAGE_MAP(CDlgHomeTab, CDialogEx)
	ON_COMMAND(IDM_RESIZE_100, &CDlgHomeTab::OnResize100)
	ON_COMMAND(IDM_ROTATION_FLIPVERTICALLY, &CDlgHomeTab::OnRotationFlipvertically)
	ON_COMMAND(IDM_ROTATION_FLIPHORIZONTALLY, &CDlgHomeTab::OnRotationFliphorizontally)
	ON_COMMAND(IDM_ROTATION_180, &CDlgHomeTab::OnRotation180)
	ON_COMMAND(IDM_ROTATION_90CCW, &CDlgHomeTab::OnRotation90ccw)
	ON_COMMAND(IDM_ROTATION_90CW, &CDlgHomeTab::OnRotation90cw)
	ON_COMMAND(IDM_FILTEREFFECT_BLUR, &CDlgHomeTab::OnFiltereffectBlur)
	ON_COMMAND(IDM_FILTEREFFECT_MEDIANBLUR, &CDlgHomeTab::OnFiltereffectMedianblur)
	ON_COMMAND(IDM_FILTEREFFECT_EDGE, &CDlgHomeTab::OnFiltereffectEdge)
	ON_COMMAND(IDM_COLOREFFECT_BRIGHTEN, &CDlgHomeTab::OnColoreffectBrighten)
	ON_COMMAND(IDM_COLOREFFECT_DARKEN, &CDlgHomeTab::OnColoreffectDarken)
	ON_COMMAND(ID_COLOREFFECT_I, &CDlgHomeTab::OnColoreffectI)
	ON_COMMAND(IDM_FILTEREFFECT_ERODING, &CDlgHomeTab::OnFiltereffectEroding)
	ON_COMMAND(IDM_FILTEREFFECT_DILATING, &CDlgHomeTab::OnFiltereffectDilating)
	ON_COMMAND(IDM_COLOREFFECT_GRAYSCALE, &CDlgHomeTab::OnColoreffectGrayscale)
	ON_COMMAND(IDM_RESIZE_200, &CDlgHomeTab::OnResize200)
	ON_COMMAND(IDM_RESIZE_400, &CDlgHomeTab::OnResize400)
	ON_COMMAND(IDM_RESIZE_640, &CDlgHomeTab::OnResize640)
	ON_COMMAND(IDM_RESIZE_800, &CDlgHomeTab::OnResize800)
	ON_COMMAND(IDM_RESIZE_1000, &CDlgHomeTab::OnResize1000)
	ON_COMMAND(IDM_RESIZE_1024, &CDlgHomeTab::OnResize1024)
	ON_BN_CLICKED(IDC_SPLIT_RESIZE, &CDlgHomeTab::OnBnClickedSplitResize)
	ON_BN_CLICKED(IDC_SPLIT_FILTER, &CDlgHomeTab::OnBnClickedSplitFilter)
	ON_COMMAND(ID_FILTEREFFECT_BLURANDEDGE, &CDlgHomeTab::OnFiltereffectBlurandedge)
	ON_BN_CLICKED(IDC_SPLIT_COLOR, &CDlgHomeTab::OnBnClickedSplitColor)
	ON_COMMAND(IDM_COLOREFFECT_CONTRASTENHANCEMENT, &CDlgHomeTab::OnColoreffectContrastenhancement)
END_MESSAGE_MAP()


// CDlgHomeTab message handlers


void CDlgHomeTab::OnBnClickedSplitResize()
{
	// TODO: Add your control notification handler code here
	CDlgResize resizeDlg(this);
	resizeDlg.DoModal();
	
}

void CDlgHomeTab::OnBnClickedSplitFilter()
{
	// TODO: Add your control notification handler code here
	//m_ctrFilterEffect.po;
}

void CDlgHomeTab::OnBnClickedSplitColor()
{
	// TODO: Add your control notification handler code here
	CDlgColor dlg(this);
	dlg.DoModal();
}

void CDlgHomeTab::OnResize100()
{
	// TODO: Add your command handler code here
	Resize(100);
	m_pDlgParent->Invalidate();
}

void CDlgHomeTab::OnResize200()
{
	// TODO: Add your command handler code here
	Resize(200);
	m_pDlgParent->Invalidate();
}


void CDlgHomeTab::OnResize400()
{
	// TODO: Add your command handler code here
	Resize(400);
	m_pDlgParent->Invalidate();
}


void CDlgHomeTab::OnResize640()
{
	// TODO: Add your command handler code here
	Resize(640);
	m_pDlgParent->Invalidate();
}


void CDlgHomeTab::OnResize800()
{
	// TODO: Add your command handler code here
	Resize(800);
	m_pDlgParent->Invalidate();
}


void CDlgHomeTab::OnResize1000()
{
	// TODO: Add your command handler code here
	Resize(1000);
	m_pDlgParent->Invalidate();
}


void CDlgHomeTab::OnResize1024()
{
	// TODO: Add your command handler code here
	Resize(1024);
	m_pDlgParent->Invalidate();
}


void CDlgHomeTab::OnRotationFlipvertically()
{
	// TODO: Add your command handler code here
	m_pPhoto->Flip(0);
	RefreshDraw();
}



void CDlgHomeTab::OnRotationFliphorizontally()
{
	// TODO: Add your command handler code here
	m_pPhoto->Flip(1);
	RefreshDraw();
}


void CDlgHomeTab::OnRotation180()
{
	// TODO: Add your command handler code here
	m_pPhoto->Flip(-1);
	m_pDlgParent->Invalidate();
}


void CDlgHomeTab::OnRotation90ccw()
{
	// TODO: Add your command handler code here
	m_pPhoto->BeginComplex();
	m_pPhoto->Transpose();
	m_pPhoto->Flip(0);
	m_pPhoto->EndComplex();
	m_pDlgParent->Invalidate();
}


void CDlgHomeTab::OnRotation90cw()
{
	// TODO: Add your command handler code here
	m_pPhoto->BeginComplex();
	m_pPhoto->Transpose();
	m_pPhoto->Flip(1);
	m_pPhoto->EndComplex();
	m_pDlgParent->Invalidate();
}


void CDlgHomeTab::OnFiltereffectBlur()
{
	// TODO: Add your command handler code here
	m_pPhoto->Blur(5);
	RefreshDraw();
}


void CDlgHomeTab::OnFiltereffectMedianblur()
{
	// TODO: Add your command handler code here
	m_pPhoto->MedianFilter(5);
	RefreshDraw();
}


void CDlgHomeTab::OnFiltereffectEdge()
{
	// TODO: Add your command handler code here
	m_pPhoto->EdgeDetect(100);
	RefreshDraw();
}


void CDlgHomeTab::OnColoreffectBrighten()
{
	// TODO: Add your command handler code here
	m_pPhoto->AdjustBrightness(70);
	RefreshDraw();
}


void CDlgHomeTab::OnColoreffectDarken()
{
	// TODO: Add your command handler code here
	m_pPhoto->AdjustBrightness(-50);
	RefreshDraw();
}


void CDlgHomeTab::OnColoreffectI()
{
	// TODO: Add your command handler code here
	m_pPhoto->Invert();
	RefreshDraw();
}


void CDlgHomeTab::OnFiltereffectEroding()
{
	// TODO: Add your command handler code here
	m_pPhoto->Eroding();
	RefreshDraw();
}


void CDlgHomeTab::OnFiltereffectDilating()
{
	// TODO: Add your command handler code here
	m_pPhoto->Dilating();
	RefreshDraw();
}


void CDlgHomeTab::OnColoreffectGrayscale()
{
	// TODO: Add your command handler code here
	m_pPhoto->Grayscale();
	RefreshDraw();
}

void CDlgHomeTab::OnFiltereffectBlurandedge()
{
	// TODO: Add your command handler code here
	CDlgFilter dlg(this);
	dlg.DoModal();
}


void CDlgHomeTab::OnColoreffectContrastenhancement()
{
	// TODO: Add your command handler code here
	m_pPhoto->AdjustContrast(1.4f,-35);
	RefreshDraw();
}
